local skynet = require "skynet"
local filelog = require "filelog"
local tabletool = require "tabletool"
local timer = require "timer"
local cardtool = require "ssscardtool"
require "pubenum"
require "sssenum"

local seatlogic = {
    server = nil,
}

function seatlogic.init(service)
    if seatlogic.server == nil then
        seatlogic.server = service
    end
end

function seatlogic.initseat(seatobj, index)
    seatobj.index = index
    seatobj.state = SSSSeatState.SEAT_STATE_NO_PLAYER
	seatobj.is_tuoguan = EBOOL.FALSE
end

function seatlogic.is_empty(seatobj)
    return (seatobj.state == SSSSeatState.SEAT_STATE_NO_PLAYER)
end

function seatlogic.is_ingame(seatobj)
	if not seatobj.rid or seatobj.rid == 0 then 
		return false
	end
	if seatobj.state == SSSSeatState.SEAT_STATE_WAIT_NEXT_GAME then
		return false
	end
	return true
end

function seatlogic.dealcards(seatobj)
    local server = seatlogic.server
	local table_data = server.table_data
    local noticemsg = {
		rid = seatobj.rid,
		seat_index = seatobj.index,
		special_card_type = seatobj.special_card_type,
		cards = seatobj.cards
	}
	if table_data.conf.table_type == SSSTableType.TABLE_TYPE_TRAIN then
		if seatobj.is_force_quit == 0 then
    		server:send_notice_to_players({seatobj.rid}, "dealcardsN", noticemsg, nil, nil)
		end
	else
		if table_data.conf.match_type ~= 0 then
			if seatobj.is_matching == false then
				server:send_notice_to_players({seatobj.rid}, "dealcardsN", noticemsg, nil, nil)
			end
		else
			server:send_notice_to_players({seatobj.rid}, "dealcardsN", noticemsg, nil, nil)
		end
	end
end

function seatlogic.onegamestart_initseat(seat)
    seat.state = SSSSeatState.SEAT_STATE_PLAYING
    seat.continue_games = seat.continue_games + 1
	seat.ready_to_time = 0
	if seat.ready_timer_id > 0 then
		timer.cleartimer(seat.ready_timer_id)
		seat.ready_timer_id = -1
	end
end

function seatlogic.resetstate(seatobj)
	if seatobj == nil then return end
	local server = seatlogic.server
	local table_data = server.table_data
	seatobj.state = SSSSeatState.SEAT_STATE_WAIT_READY
	seatobj.timeout_count = 0							---玩家操作超时次数
	seatobj.timer_id = -1
	seatobj.win = 0										---表示玩家胜利还是失败
	seatobj.is_tuoguan = SSSBOOL.FALSE
	seatobj.ready_timer_id = -1 						---准备倒计时定时器
	seatobj.ready_to_time = 0   						---准备到期时间
	seatobj.cards = {}   								---玩家手牌
	seatobj.balance_score = 0	 						----最终结算分数
	seatobj.tuoguan_punish = 0
	seatobj.followhandsnum = 0
	seatobj.holdvalue = 0 		 						----玩家货币持有量
	seatobj.dealnum = 0
	seatobj.cancel_timerid = -1							----解散牌桌定时器
	seatobj.action_type = 0
	seatobj.action_to_time = 0							----操作超时时间
	if table_data.conf.table_type ~= SSSTableType.TABLE_TYPE_FANGKA then
    	seatobj.score = 0
    end
	seatobj.final_cards = nil	
	seatobj.is_disconnected = 0	 						----玩家是否断线
	if table_data.conf.table_type == SSSTableType.TABLE_TYPE_TRAIN then
		if seatobj.is_force_quit == 1 then end
	else
		seatobj.is_force_quit = 0	 						----玩家是否强退
	end 
	seatobj.holdvalue = 0 		 						----玩家货币持有量
	seatobj.dealnum = 0 		 						----玩家中间结算量
	seatobj.is_matching = false
	seatobj.is_delay_leave = false
	seatobj.special_card_type = 0
	seatobj.deal_allwater = 0
	seatobj.sorted_items = {}   						----理牌后的牌堆
	seatobj.deal_allwater_info = {}	
	seatobj.shoot_players_list = {} 					---打枪玩家的index 和 rid
	seatobj.is_shoot_allplayers = false 				---是否全垒打
	seatobj.deal_as_special = 0
end

function seatlogic.clear_seat(seat)
	if seat == nil then return end
	seat.rid = 0
	seat.state = SSSSeatState.SEAT_STATE_NO_PLAYER
	seat.gatenode_id = ""
	seat.agent_address = -1
	seat.playerinfo.rolename = ""
	seat.playerinfo.logo=""
	seat.playerinfo.sex = 0
    seat.playerinfo.coins = 0
    seat.playerinfo.level = 0
    seat.playerinfo.diamond = 0
	seat.playerinfo.upvoted_num = 0
	seat.playerinfo.downvoted_num = 0
	seat.playerinfo.ip_address = ""
	seat.playerinfo.gps_addr = ""
	seat.playerinfo.logoframe = 0
	
	seat.timeout_count = 0							---玩家操作超时次数
	seat.timer_id = -1
	seat.win = 0										---表示玩家胜利还是失败
	seat.is_tuoguan = SSSBOOL.FALSE
	seat.ready_to_time = 0   						---准备到期时间
	seat.cards = {}   								---玩家手牌
	seat.balance_score = 0	 						----最终结算分数
	seat.tuoguan_punish = 0
	seat.cancel_timerid = -1							----解散牌桌定时器
	seat.final_cards = nil
	seat.action_type = 0
	seat.action_to_time = 0							----操作超时时间
    seat.score = 0
	seat.diamond = 0
	seat.is_disconnected = 0	 						----玩家是否断线
	seat.is_force_quit = 0	 						----玩家是否强退 
	seat.holdvalue = 0 		 						----玩家货币持有量
	seat.dealnum = 0 		 						----玩家中间结算量
	seat.qiangzhuang_times = 0						----玩家抢庄下的倍数
	seat.xiazhu_times = 0							----玩家下注倍数
	seat.train_score = 0
	seat.is_robot = false							--是否是机器人
	seat.is_mirror = false
	seat.is_matching = false
	seat.balance_total = 0
	seat.continue_games = 0
	seat.is_delay_leave = false

	if seat.ready_timer_id > 0 then
		timer.cleartimer(seat.ready_timer_id)
	end
	seat.ready_timer_id = -1
	seat.is_matching = false
	seat.special_card_type = 0
	seat.deal_allwater = 0
	seat.sorted_items = {}   						----理牌后的牌堆	
	seat.deal_allwater_info = {}
	seat.shoot_players_list = {} 					---打枪玩家的index 和 rid
	seat.is_shoot_allplayers = false 				---是否全垒打
	seat.deal_as_special = 0
end

----判断是否是特殊牌型
function seatlogic.analysis_special_type(seat)
	local server = seatlogic.server
	local table_data = server.table_data
	if seat == nil then return end
	filelog.sys_error("--------seatlogic.analysis_special_type------", seat.rid, seat.index, seat.cards, table_data.conf.is_have_joker)
	cardtool.analysis_special_card_type(seat, table_data.conf.is_have_joker)
	filelog.sys_error("--------seatlogic.special_card_type------", seat.special_card_type)
end
---检查理牌时,传上来的牌数据是否和服务器上的手牌一致
function seatlogic.check_hand_cards(seat, sorted_cards)
	local server = seatlogic.server
	local table_data = server.table_data
	if not sorted_cards or #sorted_cards ~= 3 then return false end
	local temp_cards = {}
	for index, item in ipairs(sorted_cards) do
		for _, card in ipairs(item.cards) do
			table.insert(temp_cards, card)
		end
	end
	if #temp_cards ~= #seat.cards and #temp_cards ~= 0 then 
		return false
	end
	table.sort(temp_cards, function(a,b)
		if a > b then
			return true
		else
			return false
		end
	end)
	table.sort(seat.cards, function(a,b)
		if a > b then
			return true
		else
			return false
		end
	end)
	local is_right = true
	for index, temp_card in ipairs(temp_cards) do
		if seat.cards[index] ~= temp_card then is_right = false break end
	end
	return is_right
end

---检查理牌传上来的牌型是否符合规则
function seatlogic.check_sorted_cards(seat, temp_sorted_cards)
	local server = seatlogic.server
	local table_data = server.table_data
	local is_has_joker = table_data.conf.is_have_joker
	local is_right = true
	local err_no = 0
	filelog.sys_error("--------seatlogic.check_sorted_cards---------", seat.index, seat.rid, seat.cards, temp_sorted_cards)
	---先看牌型是否都对
	for index, sorted_item in ipairs(temp_sorted_cards) do
		if sorted_item.card_type == 0 then
			is_right = false
			break
		end
		local sorted_item_temp = tabletool.deepcopy(sorted_item)
		sorted_item_temp.card_type = 0
		cardtool.analysis_normal_card_type(sorted_item_temp, is_has_joker)
		filelog.sys_error("------analysis---after----", index, sorted_item, sorted_item_temp)
		if sorted_item.card_type ~= sorted_item_temp.card_type and sorted_item.card_type ~= 0 and sorted_item_temp.card_type ~= 0 then
			is_right = false
			break
		end
		if sorted_item.card_type == 0 and sorted_item_temp.card_type ~= 0 then
			sorted_item.card_type = sorted_item_temp.card_type
		end
	end
	if is_right == false then
		err_no = 1
		return is_right, err_no 
	end
	for i = 1, #temp_sorted_cards do
		if temp_sorted_cards[i+1] then
			if temp_sorted_cards[i].card_type > temp_sorted_cards[i+1].card_type then
				is_right = false
				err_no = 2
				break
			elseif temp_sorted_cards[i].card_type == temp_sorted_cards[i+1].card_type then
				local is_bigger = cardtool.compare_two_sorted_items(temp_sorted_cards[i], temp_sorted_cards[i+1])
				if is_bigger == true then
					is_right = false
					err_no = 2
					break
				end
			end
		end
	end
	return is_right, err_no
end

function seatlogic.copy_sorted_items(seat, temp_sorted_cards)
	if not temp_sorted_cards or #temp_sorted_cards ~= 3 then return end
	seat.sorted_items = {}
	for index, sorted_item in ipairs(temp_sorted_cards) do
		seat.sorted_items[sorted_item.card_slot_index] = {
			card_slot_index = sorted_item.card_slot_index,
            card_type = sorted_item.card_type,
            deal_score = 0,
            cards = tabletool.deepcopy(sorted_item.cards),
		}
	end
end

function seatlogic.is_deal_as_special(seatone, seattwo)
	local server = seatlogic.server
	local table_data = server.table_data
	local is_dealas_special = false
	filelog.sys_error("----------seatlogic.is_deal_as_special-------", seatone.rid, seatone.special_card_type, seatone.deal_as_special,
		seattwo.rid, seattwo.special_card_type, seattwo.deal_as_special)
	if seatone.special_card_type ~= 0 then
		if seatone.deal_as_special == 1 then
			if seattwo.special_card_type == 0 then
				is_dealas_special = true 
			else
				if seattwo.deal_as_special == 1 then
					is_dealas_special = true
				else
					is_dealas_special = false
				end
			end
		else

		end
	else
		if seattwo.special_card_type ~= 0 then
			if seattwo.deal_as_special == 1 then
				is_dealas_special = true
			end
		end
	end
	return is_dealas_special
end

----跟除开自己以外的玩家进行结算输赢水数目
function seatlogic.deal_water_withallplayer(seatobj)
	if not seatobj or seatobj.rid <= 0 then return end
	filelog.sys_error("------seatlogic.deal_water_withallplayer------", seatobj.rid, seatobj.index)
	local server = seatlogic.server
	local table_data = server.table_data
	local shoot_times = table_data.conf.shoot_times   ----打枪倍数
	local sorted_cards_items = seatobj.sorted_items
	local is_has_joker = table_data.conf.is_have_joker
	if #sorted_cards_items ~= 3 then return end
	for index, seat in ipairs(table_data.seats) do
		if seat.rid ~= 0 and seat.rid ~= seatobj.rid and seat.state == SSSSeatState.SEAT_STATE_SORTCARDS_OVER then
			----先看是否有特殊牌型
			local is_deal_special = seatlogic.is_deal_as_special(seatobj, seat)
			filelog.sys_error("---------is_deal_special-----", seatobj.rid, seat.rid, is_deal_special)
			if is_deal_special == true then
			---if seatobj.special_card_type ~= 0 or seat.special_card_type ~= 0 then
				local deal_water_info = {
					seat_rid = seat.rid,
					seat_index = seat.index,
					is_shoot = false, ----是否被打枪
					is_all_shoot = false, ---是否被打全垒
					deal_water = 0,
					water_info = {},
				}
				if seatobj.special_card_type > seat.special_card_type then
					local dealwater = SSSSpecialWaterNum[tostring(seatobj.special_card_type)]
					filelog.sys_error("--------comp--special_card_type------", seatobj.special_card_type, seat.special_card_type, dealwater)
					deal_water_info.deal_water = deal_water_info.deal_water + dealwater
				elseif seatobj.special_card_type < seat.special_card_type then
					local dealwater = SSSSpecialWaterNum[tostring(seat.special_card_type)]
					filelog.sys_error("--------comp--special_card_type------", seatobj.special_card_type, seat.special_card_type, dealwater)
					deal_water_info.deal_water = deal_water_info.deal_water - dealwater
				else
					local is_bigger = cardtool.compare_special_type_cards(seatobj.cards, seat.cards, is_has_joker)
					filelog.sys_error("--------special_card_type-------is_bigger----", is_bigger)
					if is_bigger == 1 then
						local dealwater = SSSSpecialWaterNum[tostring(seatobj.special_card_type)]
						filelog.sys_error("--------comp--special_card_type222------", seatobj.special_card_type, seat.special_card_type, dealwater)
						deal_water_info.deal_water = deal_water_info.deal_water + dealwater
					elseif is_bigger == 2 then
						local dealwater = SSSSpecialWaterNum[tostring(seat.special_card_type)]
						filelog.sys_error("--------comp--special_card_type------", seatobj.special_card_type, seat.special_card_type, dealwater)
						deal_water_info.deal_water = deal_water_info.deal_water - dealwater
					end
				end
				seatobj.deal_allwater_info[seat.rid] = tabletool.deepcopy(deal_water_info)
			else
				local temp_sorted_items = seat.sorted_items
				if #temp_sorted_items == 3 then
					---先比前道
					local win_or_lose_times = 0
					local deal_water_info = {
						seat_rid = seat.rid,
						seat_index = seat.index,
						is_shoot = false, ----打枪
						is_be_shoot = false, -----被打枪
						is_all_shoot = false, ---是否被打全垒
						deal_water = 0,
						no_add_deal_water = 0,
						water_info = {},
					}
					deal_water_info.water_info[1] = {
						deal_water = 0,
						card_type = 0,
					}
					local my_item_one = sorted_cards_items[1]
					local other_item_one = temp_sorted_items[1]
					local is_bigger = cardtool.compare_two_sorted_items(my_item_one, other_item_one)
					--filelog.sys_error("--------my_item_one------", my_item_one, other_item_one, is_bigger)
					deal_water_info.water_info[1].card_type = other_item_one.card_type
					if is_bigger == 1 then
						win_or_lose_times = win_or_lose_times + 1
						---冲三 +3水
						if my_item_one.card_type == SSSCardType.CARD_TYPE_SANTIAO then
							deal_water_info.water_info[1].deal_water = deal_water_info.water_info[1].deal_water + 3
						else
							deal_water_info.water_info[1].deal_water = deal_water_info.water_info[1].deal_water + 1
						end
					elseif is_bigger == 2 then
						win_or_lose_times = win_or_lose_times - 1
						if other_item_one.card_type == SSSCardType.CARD_TYPE_SANTIAO then
							deal_water_info.water_info[1].deal_water = deal_water_info.water_info[1].deal_water - 3
						else
							deal_water_info.water_info[1].deal_water = deal_water_info.water_info[1].deal_water - 1
						end
					end
					---再比中道
					deal_water_info.water_info[2] = {
						deal_water = 0,
						card_type = 0,
					}
					my_item_one = sorted_cards_items[2]
					other_item_one = temp_sorted_items[2]
					is_bigger = cardtool.compare_two_sorted_items(my_item_one, other_item_one)
					--filelog.sys_error("-------my_item_one two------", my_item_one, other_item_one, is_bigger)
					deal_water_info.water_info[2].card_type = other_item_one.card_type
					if is_bigger == 1 then
						win_or_lose_times = win_or_lose_times + 1
						if my_item_one.card_type == SSSCardType.CARD_TYPE_HULU then
							--中道 葫芦 + 2 水
							deal_water_info.water_info[2].deal_water = deal_water_info.water_info[2].deal_water + 2
						elseif my_item_one.card_type == SSSCardType.CARD_TYPE_TIEZHI then
							--中墩 铁支 +7水
							deal_water_info.water_info[2].deal_water = deal_water_info.water_info[2].deal_water + 7
						elseif my_item_one.card_type == SSSCardType.CARD_TYPE_TONGHUASHUN then
							--中墩 同花顺 +9水
							deal_water_info.water_info[2].deal_water = deal_water_info.water_info[2].deal_water + 9
						elseif my_item_one.card_type == SSSCardType.CARD_TYPE_WUTONG then
							--中敦 五同 +11水
							deal_water_info.water_info[2].deal_water = deal_water_info.water_info[2].deal_water + 11
						else
							deal_water_info.water_info[2].deal_water = deal_water_info.water_info[2].deal_water + 1
						end
						elseif is_bigger == 2 then
						win_or_lose_times = win_or_lose_times - 1
						if other_item_one.card_type == SSSCardType.CARD_TYPE_HULU then
							--中道 葫芦 + 2 水
							deal_water_info.water_info[2].deal_water = deal_water_info.water_info[2].deal_water - 2
						elseif other_item_one.card_type == SSSCardType.CARD_TYPE_TIEZHI then
							--中墩 铁支 +7水
							deal_water_info.water_info[2].deal_water = deal_water_info.water_info[2].deal_water - 7
						elseif other_item_one.card_type == SSSCardType.CARD_TYPE_TONGHUASHUN then
							--中墩 同花顺 +9水
							deal_water_info.water_info[2].deal_water = deal_water_info.water_info[2].deal_water - 9
						elseif other_item_one.card_type == SSSCardType.CARD_TYPE_WUTONG then
							--中敦 五同 +11水
							deal_water_info.water_info[2].deal_water = deal_water_info.water_info[2].deal_water - 11
						else
							deal_water_info.water_info[2].deal_water = deal_water_info.water_info[2].deal_water - 1
						end
					end
					---最后比后道
					deal_water_info.water_info[3] = {
						deal_water = 0,
						card_type = 0,
					}
					my_item_one = sorted_cards_items[3]
					other_item_one = temp_sorted_items[3]
					is_bigger = cardtool.compare_two_sorted_items(my_item_one, other_item_one)
					--filelog.sys_error("-------my_item_one three------", my_item_one, other_item_one, is_bigger)
					deal_water_info.water_info[3].card_type = other_item_one.card_type
					if is_bigger == 1 then
						win_or_lose_times = win_or_lose_times + 1
						if my_item_one.card_type == SSSCardType.CARD_TYPE_TIEZHI then
							--尾墩 铁支 +4水
							deal_water_info.water_info[3].deal_water = deal_water_info.water_info[3].deal_water + 4
						elseif my_item_one.card_type == SSSCardType.CARD_TYPE_TONGHUASHUN then
							--尾墩 同花顺 +5水
							deal_water_info.water_info[3].deal_water = deal_water_info.water_info[3].deal_water + 5
						elseif my_item_one.card_type == SSSCardType.CARD_TYPE_WUTONG then
							--尾敦 五同 +6水
							deal_water_info.water_info[3].deal_water = deal_water_info.water_info[3].deal_water + 6
						else
							deal_water_info.water_info[3].deal_water = deal_water_info.water_info[3].deal_water + 1
						end
					elseif is_bigger == 2 then
						win_or_lose_times = win_or_lose_times - 1
						if other_item_one.card_type == SSSCardType.CARD_TYPE_TIEZHI then
							--尾墩 铁支 +4水
							deal_water_info.water_info[3].deal_water = deal_water_info.water_info[3].deal_water - 4
						elseif other_item_one.card_type == SSSCardType.CARD_TYPE_TONGHUASHUN then
							--尾墩 同花顺 +5水
							deal_water_info.water_info[3].deal_water = deal_water_info.water_info[3].deal_water - 5
						elseif other_item_one.card_type == SSSCardType.CARD_TYPE_WUTONG then
							--尾敦 五同 +6水
							deal_water_info.water_info[3].deal_water = deal_water_info.water_info[3].deal_water - 6
						else
							deal_water_info.water_info[3].deal_water = deal_water_info.water_info[3].deal_water - 1
						end
					end
					local deal_water_three = 0
					for index, waterinfo in ipairs(deal_water_info.water_info) do
						deal_water_info.deal_water = deal_water_info.deal_water + waterinfo.deal_water
						deal_water_info.no_add_deal_water = deal_water_info.no_add_deal_water + waterinfo.deal_water
						temp_sorted_items[index].deal_score = temp_sorted_items[index].deal_score - waterinfo.deal_water
					end
					---是否打枪
					filelog.sys_error("-------------win_or_lose_times----------", win_or_lose_times)
					if win_or_lose_times == 3 or win_or_lose_times == -3 then
						-- if win_or_lose_times == 3 then
						-- 	deal_water_info.is_shoot = true
						-- elseif win_or_lose_times == -3 then
						-- 	deal_water_info.is_be_shoot = true
						-- end
						deal_water_info.is_shoot = true
						local begin_value = deal_water_info.deal_water
						deal_water_info.deal_water = deal_water_info.deal_water * shoot_times
						local add_value = deal_water_info.deal_water - begin_value
						if win_or_lose_times == 3 then
							table.insert(seatobj.shoot_players_list, {rid = seat.rid, index = seat.index})
							local shoot_info_base = {
								shoot_player_rid 	= seatobj.rid,
								shoot_player_index 	= seatobj.index,
								shoot_player_win_water = add_value,
								be_shoot_player_rid = seat.rid,
								be_shoot_player_index = seat.index,
								be_shoot_player_lose_water = - add_value,
							}
							table.insert(table_data.all_player_shootinfos, shoot_info_base)
						end
					end
					seatobj.deal_allwater_info[seat.rid] = tabletool.deepcopy(deal_water_info)
				end
			end
		end
	end
	filelog.sys_error("---------seatobj.deal_allwater_info---------", seatobj.rid, seatobj.deal_allwater_info)
end
---汇总 总输赢水数
function seatlogic.deal_seatallwaternum(seatobj)
	local server = seatlogic.server
	local table_data = server.table_data
	filelog.sys_error("-------seatlogic.deal_seatallwaternum-----", seatobj.rid, seatobj.deal_allwater)
	for otherrid, deal_info in pairs(seatobj.deal_allwater_info) do
		seatobj.deal_allwater = seatobj.deal_allwater + deal_info.deal_water
	end
end

function seatlogic.check_auto_sorted_item(seatobj, sorted_item_temp)
	local server = seatlogic.server
	local table_data = server.table_data
	if not sorted_item_temp or #sorted_item_temp ~= 3 then
		return false
	end
	for index, sorted_item in ipairs(sorted_item_temp) do
		if index == 1 then
			if not sorted_item.cards or #sorted_item.cards ~= 3 then
				return false
			end
		else
			if not sorted_item.cards or #sorted_item.cards ~= 5 then
				return false
			end
		end
	end
	return true
end

---自动理牌
function seatlogic.auto_sorted_cards(seatobj)
	local server = seatlogic.server
	local table_data = server.table_data
	local is_has_joker = table_data.conf.is_have_joker
	filelog.sys_error("-------seatlogic.auto_sorted_cards--------", seatobj.rid, seatobj.index, seatobj.cards, seatobj.sorted_items, 
		seatobj.special_card_type, seatobj.deal_as_special, is_has_joker)
	local sorted_item_temp = cardtool.sort_hand_cards(seatobj.cards, is_has_joker, seatobj.special_card_type, seatobj.deal_as_special)
	filelog.sys_error("------seatlogic----sorted_item_temp-----", sorted_item_temp)
	local is_sorted_ok = seatlogic.check_auto_sorted_item(seatobj, sorted_item_temp)
	if is_sorted_ok == false then
		filelog.sys_error("------------auto_sorted_failed-------------", seatobj.rid, seatobj.cards)
	end
	seatlogic.copy_sorted_items(seatobj, sorted_item_temp)
end

return seatlogic
